#include "rendering/RootNode.h"
#include <sstream>


using namespace Boon;
using namespace Rendering;




Boon::Rendering::RootNode::RootNode( void )
	: SceneNode( GameObject::INVALID_ID, "Root", PASS_0 )
{
	// Groups accessible with enum: RenderPass
	ISceneNodeStrong groupStatic( new SceneNode( GameObject::INVALID_ID, "Pass_Static", PASS_STATIC ) );
	ISceneNodeStrong groupGO( new SceneNode( GameObject::INVALID_ID, "Pass_GameObject", PASS_STATIC ) );
	ISceneNodeStrong groupSky( new SceneNode( GameObject::INVALID_ID, "Pass_Sky", PASS_STATIC ) );
	ISceneNodeStrong groupAlpha( new SceneNode( GameObject::INVALID_ID, "Pass_Alpha", PASS_STATIC ) );
	ISceneNodeStrong groupNotRendered( new SceneNode( GameObject::INVALID_ID, "Pass_NotRendered", PASS_NOT_RENDERED ) );

	VecSceneNodes& childs = GetChilds();
	childs.push_back( groupStatic );
	childs.push_back( groupGO );
	childs.push_back( groupSky );
	childs.push_back( groupAlpha );
	childs.push_back( groupNotRendered );
}

bool Boon::Rendering::RootNode::IsVisible( Scene* scene ) const
{
	return true;
}

bool Boon::Rendering::RootNode::AddChild( const ISceneNodeStrong& child )
{
	RenderPass pass = child->Get()->GetRenderPass();
	VecSceneNodes& childs = GetChilds();

	if( (uint_32)pass < childs.size() && pass >= (uint_32)PASS_STATIC )
		return childs[pass]->AddChild( child );

	std::ostringstream oss( "Could not resolve renderpass-node from: " );
	oss << child->Get()->Name() << " ID: " << pass;
	throw BoonException( "RootNode", "AddChild", oss.str().c_str() );
	return false;
}

bool Boon::Rendering::RootNode::RenderChildren( Scene* scene )
{
	for( int i = PASS_0; i < PASS_LAST; ++i )
	{
		// todo: change rendering modes
		switch( i )
		{
		case PASS_STATIC:
		case PASS_GAMEOBECT:
			GetChilds()[i]->RenderChildren( scene );
			break;
		case PASS_SKY:
			GetChilds()[i]->RenderChildren( scene );
			break;
		case PASS_ALPHA:
			GetChilds()[i]->RenderChildren( scene );
		case PASS_NOT_RENDERED:
			GetChilds()[i]->RenderChildren( scene );
			break;
		}
	}
	return true;
}


RootNode::~RootNode(void)
{
}
